GLOBAL_WORKSPACE:=$(CURDIR)/.workspace
GLOBAL_VIRTUALENV_DIR:=$(CURDIR)/.venv

DIRECTORY:=$(CURDIR)
WORKSPACE_NAME:=tools
include $(CURDIR)/makelibs/commons.mk

PLATFORM_CONFIG:=$(CURDIR)/platform-config.yml
NAUTA_CONFIG:=$(CURDIR)/nauta-config.yml
COMPILER_CONFIG:=$(CURDIR)/compiler-config.yml
RPM_CONFIG:=$(CURDIR)/rpm-config.yml
SHARED_CONFIG:=$(CURDIR)/shared-config.yml
BIN_CONFIG:=$(CURDIR)/bin-config.yml

PUSH_MAKE:=(cd $(CURDIR)/push && make build)
RPMS_MAKE:=(cd $(CURDIR)/rpms-build && make build)
CHART_MAKE:=(cd $(CURDIR)/chart-composer && make build)
CONTAINER_MAKE:=(cd $(CURDIR)/container-build && make build)

FINALIZERS_PLATFORM_MAKE:=(cd $(CURDIR)/finalizers/platform && make build)
FINALIZERS_NAUTA_MAKE:=(cd $(CURDIR)/finalizers/nauta && make build)
FINALIZERS_BIN_MAKE:=(cd $(CURDIR)/finalizers/bin && make build)
FINALIZERS_DOCS_MAKE:=(cd $(CURDIR)/finalizers/docs && make build)
FINALIZERS_RELEASE_MAKE:=(cd $(CURDIR)/finalizers/release && make build)
FINALIZERS_RPM_MAKE:=(cd $(CURDIR)/finalizers/rpm && make build)

DOCKER_PACKAGE:=$(BUILD_DIR)/docker.tar.gz
PLATFORM_PACKAGE:=$(BUILD_DIR)/platform.tar.gz
PLATFORM_CHART:=$(BUILD_DIR)/platform-chart.tar.gz
COMPILER_PACKAGE:=$(BUILD_DIR)/compiler.tar.gz
NAUTA_PACKAGE:=$(BUILD_DIR)/nauta.tar.gz
NAUTA_CHART:=$(BUILD_DIR)/nauta-chart.tar.gz
BIN_PACKAGE:=$(BUILD_DIR)/bin.tar.gz

SHARED_PACKAGE:=$(BUILD_DIR)/shared.tar.gz
RPM_PACKAGE:=$(BUILD_DIR)/rpm.tar.gz

PLATFORM_RELEASE_PACKAGE:=$(BUILD_DIR)/platform-release.tar.gz
BIN_RELEASE_PACKAGE:=$(BUILD_DIR)/bin-release.tar.gz
NAUTA_RELEASE_PACKAGE:=$(BUILD_DIR)/nauta-release.tar.gz
DOCS_RELEASE_PACKAGE:=$(BUILD_DIR)/docs-release.tar.gz
RPM_RELEASE_PACKAGE:=$(BUILD_DIR)/rpm-release.tar.gz

RELEASE_PACKAGE:=$(WORKSPACE)/nauta-$(VERSION).tar.gz
RELEASE_PACKAGE_CHECKSUM:=$(WORKSPACE)/nauta-$(VERSION).tar.gz.sha256sum
REMOTE_RELEASE_PACKAGE:=releases/nauta/nauta-$(VERSION).tar.gz
REMOTE_RELEASE_PACKAGE_CHECKSUM:=releases/nauta/nauta-$(VERSION).tar.gz.sha256sum

DOCS:=$(CURDIR)/docs

PLATFORM_DEPLOYER:=$(CURDIR)/deployers/platform
NAUTA_DEPLOYER:=$(CURDIR)/deployers/nauta
RELEASE_DEPLOYER:=$(CURDIR)/deployers/release

# Global exports
export VERSION_MAJOR := $(DEFAULT_VERSION_MAJOR)
export VERSION_MINOR := $(DEFAULT_VERSION_MINOR)
export VERSION_NO := $(DEFAULT_VERSION_NO)
export VERSION_ID := $(DEFAULT_VERSION_ID)
export BUILD_OPTIONS := $(BUILD_OPTIONS)
export GLOBAL_WORKSPACE := $(GLOBAL_WORKSPACE)
export GLOBAL_VIRTUALENV_DIR := $(GLOBAL_VIRTUALENV_DIR)

release-package: $(RELEASE_PACKAGE)
release-package: export ENV_SRC := $(RELEASE_PACKAGE)
release-package: export ENV_DEST := $(REMOTE_RELEASE_PACKAGE)
release-package: export BUILD_LOGS := $(BUILD_LOGS)
release-package: export ENV_OUTPUT_ARTIFACT_DIRECTORY := $(ENV_OUTPUT_ARTIFACT_DIRECTORY)
release-package:
ifneq ($(ENV_OUTPUT_ARTIFACT_DIRECTORY),)
	@$(PUSH_MAKE)
endif

release-checksum: $(RELEASE_PACKAGE)
release-checksum: export ENV_SRC := $(RELEASE_PACKAGE_CHECKSUM)
release-checksum: export ENV_DEST := $(REMOTE_RELEASE_PACKAGE_CHECKSUM)
release-checksum: export BUILD_LOGS := $(BUILD_LOGS)
release-checksum: export ENV_OUTPUT_ARTIFACT_DIRECTORY := $(ENV_OUTPUT_ARTIFACT_DIRECTORY)
release-checksum:
ifeq (True,$(ENV_CALCULATESUM))
	sha256sum "$(RELEASE_PACKAGE)" > "$(RELEASE_PACKAGE).sha256sum"
	@$(PUSH_MAKE)
endif

release: release-package release-checksum

push: ENV_SRC ENV_DEST
push: export ENV_SRC := $(ENV_SRC)
push: export ENV_DEST := $(ENV_DEST)
push: export BUILD_LOGS := $(BUILD_LOGS)
push: export ENV_OUTPUT_ARTIFACT_DIRECTORY := $(ENV_OUTPUT_ARTIFACT_DIRECTORY)
push:
ifneq ($(ENV_OUTPUT_ARTIFACT_DIRECTORY),)
	@$(PUSH_MAKE)
endif


build: $(RELEASE_PACKAGE)

$(RELEASE_PACKAGE): $(PLATFORM_RELEASE_PACKAGE) $(NAUTA_RELEASE_PACKAGE) $(DOCS_RELEASE_PACKAGE) $(BIN_RELEASE_PACKAGE)
$(RELEASE_PACKAGE): export NAME := release
$(RELEASE_PACKAGE): export ENV_OUTPUT := $(RELEASE_PACKAGE)
$(RELEASE_PACKAGE): export ENV_PLATFORM_PACKAGE := $(PLATFORM_RELEASE_PACKAGE)
$(RELEASE_PACKAGE): export ENV_DOCS_PACKAGE := $(DOCS_RELEASE_PACKAGE)
$(RELEASE_PACKAGE): export ENV_NAUTA_PACKAGE := $(NAUTA_RELEASE_PACKAGE)
$(RELEASE_PACKAGE): export ENV_BIN_PACKAGE := $(BIN_RELEASE_PACKAGE)
$(RELEASE_PACKAGE): export ENV_DEPLOYER := $(RELEASE_DEPLOYER)
$(RELEASE_PACKAGE):
	@mkdir -p $(BUILD_DIR)
	@$(FINALIZERS_RELEASE_MAKE)


$(PLATFORM_RELEASE_PACKAGE): export ENV_BUILD_CONFIG := $(PLATFORM_CONFIG)
$(PLATFORM_RELEASE_PACKAGE): export NAME := platform
$(PLATFORM_RELEASE_PACKAGE): $(PLATFORM_CHART) $(PLATFORM_PACKAGE) $(RPM_RELEASE_PACKAGE)
$(PLATFORM_RELEASE_PACKAGE): export ENV_PLATFORM_PACKAGE := $(PLATFORM_PACKAGE)
$(PLATFORM_RELEASE_PACKAGE): export ENV_RPM_PACKAGE := $(RPM_RELEASE_PACKAGE)
$(PLATFORM_RELEASE_PACKAGE): export ENV_DEPLOYER := $(PLATFORM_DEPLOYER)
$(PLATFORM_RELEASE_PACKAGE): export ENV_CHART := $(PLATFORM_CHART)
$(PLATFORM_RELEASE_PACKAGE): export ENV_OUTPUT := $(PLATFORM_RELEASE_PACKAGE)
$(PLATFORM_RELEASE_PACKAGE):
	@mkdir -p $(BUILD_DIR)
	@$(FINALIZERS_PLATFORM_MAKE)

$(DOCS_RELEASE_PACKAGE): export ENV_BUILD_CONFIG := $(PLATFORM_CONFIG)
$(DOCS_RELEASE_PACKAGE): export NAME := docs
$(DOCS_RELEASE_PACKAGE): export ENV_DOCS_PACKAGE := $(DOCS)
$(DOCS_RELEASE_PACKAGE): export ENV_OUTPUT := $(DOCS_RELEASE_PACKAGE)
$(DOCS_RELEASE_PACKAGE):
	@mkdir -p $(BUILD_DIR)
	@$(FINALIZERS_DOCS_MAKE)

$(RPM_RELEASE_PACKAGE): export ENV_BUILD_CONFIG := $(RPM_CONFIG)
$(RPM_RELEASE_PACKAGE): export NAME := rpm
$(RPM_RELEASE_PACKAGE): $(RPM_PACKAGE) $(PLATFORM_PACKAGE)
$(RPM_RELEASE_PACKAGE): export ENV_RPM_PACKAGE := $(RPM_PACKAGE)
$(RPM_RELEASE_PACKAGE): export ENV_PLATFORM_PACKAGE := $(PLATFORM_PACKAGE)
$(RPM_RELEASE_PACKAGE): export ENV_OUTPUT := $(RPM_RELEASE_PACKAGE)
$(RPM_RELEASE_PACKAGE):
	@mkdir -p $(BUILD_DIR)
	@$(FINALIZERS_RPM_MAKE)

$(RPM_PACKAGE): $(SHARED_PACKAGE)
$(RPM_PACKAGE): export INPUT_PACKAGE := $(SHARED_PACKAGE)
$(RPM_PACKAGE): export ENV_OUTPUT := $(RPM_PACKAGE)
$(RPM_PACKAGE):
	@mkdir -p $(BUILD_DIR)
	@$(CONTAINER_MAKE)

$(NAUTA_RELEASE_PACKAGE): export ENV_BUILD_CONFIG := $(NAUTA_CONFIG)
$(NAUTA_RELEASE_PACKAGE): export NAME := nauta
$(NAUTA_RELEASE_PACKAGE): $(NAUTA_PACKAGE) $(NAUTA_CHART)
$(NAUTA_RELEASE_PACKAGE): export ENV_OUTPUT := $(NAUTA_RELEASE_PACKAGE)
$(NAUTA_RELEASE_PACKAGE): export ENV_PLATFORM_PACKAGE := $(NAUTA_PACKAGE)
$(NAUTA_RELEASE_PACKAGE): export ENV_DEPLOYER := $(NAUTA_DEPLOYER)
$(NAUTA_RELEASE_PACKAGE): export ENV_CHART := $(NAUTA_CHART)
$(NAUTA_RELEASE_PACKAGE):
	@mkdir -p $(BUILD_DIR)
	$(FINALIZERS_NAUTA_MAKE)

$(NAUTA_CHART): $(NAUTA_PACKAGE)
$(NAUTA_CHART): export ENV_OUTPUT := $(NAUTA_CHART)
$(NAUTA_CHART): export INPUT_PACKAGE := $(NAUTA_PACKAGE)
$(NAUTA_CHART):
	@mkdir -p $(BUILD_DIR)
	@$(CHART_MAKE)

$(NAUTA_PACKAGE): $(SHARED_PACKAGE)
$(NAUTA_PACKAGE): export INPUT_PACKAGE := $(SHARED_PACKAGE)
$(NAUTA_PACKAGE): export ENV_OUTPUT := $(NAUTA_PACKAGE)
$(NAUTA_PACKAGE):
	@mkdir -p $(BUILD_DIR)
	@$(CONTAINER_MAKE)

$(PLATFORM_CHART): $(PLATFORM_PACKAGE)
$(PLATFORM_CHART): export ENV_OUTPUT := $(PLATFORM_CHART)
$(PLATFORM_CHART): export INPUT_PACKAGE := $(PLATFORM_PACKAGE)
$(PLATFORM_CHART):
	@mkdir -p $(BUILD_DIR)
	@$(CHART_MAKE)

$(PLATFORM_PACKAGE): $(SHARED_PACKAGE)
$(PLATFORM_PACKAGE): export INPUT_PACKAGE := $(SHARED_PACKAGE)
$(PLATFORM_PACKAGE): export ENV_OUTPUT := $(PLATFORM_PACKAGE)
$(PLATFORM_PACKAGE):
	@mkdir -p $(BUILD_DIR)
	@$(CONTAINER_MAKE)

$(BIN_RELEASE_PACKAGE): export ENV_BUILD_CONFIG := $(BIN_CONFIG)
$(BIN_RELEASE_PACKAGE): export NAME := bin
$(BIN_RELEASE_PACKAGE): $(BIN_PACKAGE)
$(BIN_RELEASE_PACKAGE): export ENV_PLATFORM_PACKAGE := $(BIN_PACKAGE)
$(BIN_RELEASE_PACKAGE): export ENV_OUTPUT := $(BIN_RELEASE_PACKAGE)
$(BIN_RELEASE_PACKAGE):
	@mkdir -p $(BUILD_DIR)
	$(FINALIZERS_BIN_MAKE)

$(BIN_PACKAGE): $(SHARED_PACKAGE)
$(BIN_PACKAGE): export INPUT_PACKAGE := $(SHARED_PACKAGE)
$(BIN_PACKAGE): export ENV_OUTPUT := $(BIN_PACKAGE)
$(BIN_PACKAGE):
	@mkdir -p $(BUILD_DIR)
	@$(CONTAINER_MAKE)

$(SHARED_PACKAGE): export ENV_BUILD_CONFIG := $(SHARED_CONFIG)
$(SHARED_PACKAGE): export NAME := shared
$(SHARED_PACKAGE): export ENV_OUTPUT := $(SHARED_PACKAGE)
$(SHARED_PACKAGE): export INPUT_PACKAGE :=
$(SHARED_PACKAGE):
	@mkdir -p $(BUILD_DIR)
	@$(CONTAINER_MAKE)
